package com.mye.prometheusexportdcim.controller;


import com.mye.prometheusexportdcim.service.MetricsService;
import com.mye.prometheusexportdcim.utils.LogLocalityUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


/**
 * @author hch
 * @创建人 hch
 * @创建时间 2021/5/25 11:24
 * @描述 prometheus  exportor
 * @版本 1.0.0
 */

@Slf4j
@RestController
public class SnmpCollectController {


    @Autowired
    MetricsService metricsService;


    /**
     * 之前的废弃
     * @return
     */

//    @RequestMapping(value = "/{id}/metrics", method = RequestMethod.GET)
//    public ResponseEntity<String> metrics(@PathVariable("id") int id) {
//        log.info("id=========" + id);
//        long t1 = System.currentTimeMillis();
//        ResponseEntity<String> metrics = metricsService.metrics(id);
//        long t2 = System.currentTimeMillis();
//        System.out.println(id + "===花费时间===" + (t2 - t1));
//        return metrics;
//
//    }


    /**
     * 通过snmp协议获取采集主机oid的值，供TDengine 采集存储
     *
     * @return
     */
    @GetMapping
    public ResponseEntity<String> metrics() {
        long t1 = System.currentTimeMillis();
        log.info(LogLocalityUtil.getLogLocality() + "获取指标值请求开始:{} ", t1);
        ResponseEntity<String> metrics = metricsService.metrics();
        long t2 = System.currentTimeMillis();
        log.info(LogLocalityUtil.getLogLocality() + "获取指标值请求结束：{}, 花费时间:{} ", t2, (t2 - t1));
        return metrics;

    }


    /**
     * 实例心跳检查
     *
     * @return
     */
    @GetMapping("/ping")
    public ResponseEntity<String> heartbeat() {
        long t1 = System.currentTimeMillis();
        log.info(LogLocalityUtil.getLogLocality() + " 心跳检查请求开始:{} ", t1);
        ResponseEntity<String> metrics = metricsService.heartbeat();
        long t2 = System.currentTimeMillis();
        log.info(LogLocalityUtil.getLogLocality() + "心跳请求结束：{}, 花费时间:{} ", t2, (t2 - t1));
        return metrics;


    }


}
